 /* This file is part of InvoiceSplitter for Android
  *
  * InvoiceSplitter for Android is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as published by
  * the Free Software Foundation.
  *
  * InvoiceSplitter for Android is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; withiout even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with InvoiceSplitter for Android.  If not, see <http://www.gnu.org/licenses/>.
  */
package se.gunneras.invoicesplitter;

import se.gunneras.invoicesplitter.R;
import android.content.ContentResolver;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.database.Cursor;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.provider.Contacts.People;
import android.os.Bundle;

public class InvoiceSplitter extends PreferenceActivity implements OnSharedPreferenceChangeListener {
    public static final String PRIVATE_CALL_PREFIX_KEY = "private_call_prefix";
    public static final String INTERNATIONAL_PREFIX_KEY = "international_prefix";
    public static final String ENABLE_CALL_LOG_CORRECTION_KEY = "enable_call_log_correction";
    public static final String BROKEN_PRIVATE_CALL_PREFIX_KEY = "broken_private_call_prefix";
    public static final String INCOMING_TO_MOBILE_PREFIX_KEY = "incoming_to_mobile_number_prefix";
    public static final String PRIVATE_CONTACTS_KEY = "private_contacts";
    
    public static final String PRIVATE_CONTACT_KEY_PREFIX = "PrivateContact:";

	private EditTextPreference mPrivateCallPrefixPreference;
	private EditTextPreference mInternationalPrefixPreference;
	private EditTextPreference mBrokenPrivateCallPrefixPreference;
	private EditTextPreference mIncomingToMobilePrefixPreference;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.layout.settings);
        
        //Save references to preferences
        mPrivateCallPrefixPreference = (EditTextPreference)getPreferenceScreen().findPreference(PRIVATE_CALL_PREFIX_KEY);
        mInternationalPrefixPreference = (EditTextPreference)getPreferenceScreen().findPreference(INTERNATIONAL_PREFIX_KEY);
        mBrokenPrivateCallPrefixPreference = (EditTextPreference)getPreferenceScreen().findPreference(BROKEN_PRIVATE_CALL_PREFIX_KEY);
        mIncomingToMobilePrefixPreference = (EditTextPreference)getPreferenceScreen().findPreference(INCOMING_TO_MOBILE_PREFIX_KEY);
        
        PreferenceScreen preferenceScreen = (PreferenceScreen)getPreferenceScreen().findPreference(PRIVATE_CONTACTS_KEY);
        String[] contactColumns = new String[] {
        							People._ID,
        							People.NAME
        						};
        Cursor contactsCursor = managedQuery(People.CONTENT_URI, contactColumns, null, null, People.NAME + " ASC");
        
        if (contactsCursor.moveToFirst()) {
        	int nameColumn = contactsCursor.getColumnIndex(People.NAME);
        	int idColumn = contactsCursor.getColumnIndex(People._ID);
        	do {
        		CheckBoxPreference contact = new CheckBoxPreference(this);
        		contact.setKey(PRIVATE_CONTACT_KEY_PREFIX + contactsCursor.getDouble(idColumn));
        		contact.setTitle(contactsCursor.getString(nameColumn));
        		// TODO: Selected accordning to saved preference
            	preferenceScreen.addPreference(contact);        		
        	} while (contactsCursor.moveToNext());
        }
    }
    
    @Override
    public void onResume() {
    	super.onResume();
    	SharedPreferences mSharedPreferences = getPreferenceScreen().getSharedPreferences();
    	
    	mPrivateCallPrefixPreference.setSummary(mSharedPreferences.getString(PRIVATE_CALL_PREFIX_KEY, ""));
    	mInternationalPrefixPreference.setSummary(mSharedPreferences.getString(INTERNATIONAL_PREFIX_KEY, ""));
    	mBrokenPrivateCallPrefixPreference.setSummary(mSharedPreferences.getString(BROKEN_PRIVATE_CALL_PREFIX_KEY, ""));
    	mIncomingToMobilePrefixPreference.setSummary(mSharedPreferences.getString(INCOMING_TO_MOBILE_PREFIX_KEY, ""));
    	
        // Set up a listener whenever a key changes            
        mSharedPreferences.registerOnSharedPreferenceChangeListener(this);
    }
    
    @Override
    public void onPause() {
    	super.onPause();
    	
        // Unregister the listener whenever a key changes            
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);    

    }
    
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals(PRIVATE_CALL_PREFIX_KEY)) {
        	mPrivateCallPrefixPreference.setSummary((sharedPreferences).getString(key, ""));
        }
        else if (key.equals(INTERNATIONAL_PREFIX_KEY)) {
        	mInternationalPrefixPreference.setSummary((sharedPreferences).getString(key, ""));
        }
        else if (key.equals(BROKEN_PRIVATE_CALL_PREFIX_KEY)) {
        	mBrokenPrivateCallPrefixPreference.setSummary((sharedPreferences).getString(key, ""));
        }
        else if (key.equals(INCOMING_TO_MOBILE_PREFIX_KEY)) {
        	mIncomingToMobilePrefixPreference.setSummary((sharedPreferences).getString(key, ""));
        }
        else if (key.startsWith(PRIVATE_CONTACT_KEY_PREFIX)) {
        	//TODO: Add key.substring(PRIVATE_CONTACT_KEY_PREFIX.length()) as a private contact.
        }
    }
}